<?php
/**
 * Created by PhpStorm.
 * User: liuwei
 * Date: 2022/12/16
 * Time: 14:19
 */

namespace App\Http\Controllers\Api;

use App\Http\Requests\ApiV2\ProblemRequest;
use App\Http\Requests\ApiV2\SolutionRequest;

use App\Services\Business\Models\Attach;
use App\Services\Business\Models\Solution;
use Illuminate\Http\Request;
use App\Services\Business\Models\Problem;
use App\Services\Business\Services\SolutionService;
use App\Services\Business\Interfaces\SolutionServiceInterface;
use App\Services\Business\Interfaces\RecommendServiceInterface;
use App\Services\Business\Services\RecommendService;
use App\Services\Business\Interfaces\AttachServiceInterface;
use App\Services\Business\Services\AttachService;

use Illuminate\Support\Facades\DB;

class ProblemController extends BaseController
{
    /**
     * @var SolutionService
     */
    protected $solutionService;
    /**
     * @var RecommendService
     */
    protected $recommendService;
    /**
     * @var AttachService
     */
    protected $attachService;


    public function __construct(
        SolutionServiceInterface  $solutionService,
        AttachServiceInterface    $attachService,
        RecommendServiceInterface $recommendService

    )
    {

        $this->solutionService = $solutionService;
        $this->attachService = $attachService;
        $this->recommendService = $recommendService;


    }

    public function index(Request $request)
    {
        $page = $request->input('page', 1);
        $size = $request->input('size', 20);
        $query = Problem::query()
            ->with(['user'])
            ->orderByDesc('id');
        $total = $query->count();
        $problems = $query->forPage($page, $size)->get()->toArray();

        return $this->successData(compact('problems', 'total'));
    }

    public function store(ProblemRequest $request, Problem $problem)
    {


        $data = $request->filldata();
        $problem->fill($data)->save();
        $data['tags'] && $problem->tags()->attach($data['tags']);
        //附件
        $data['attaches'] && $problem->attaches()->attach($data['attaches']);
        //录屏
        $data['record_ids'] && $problem->records()->attach($data['record_ids']);
        return $this->success("成功");
    }

    public function update(ProblemRequest $request, $id)
    {
        $problem = Problem::query()->where('id', $id)->firstOrFail();
        $data = $request->filldata();
        DB::transaction(function () use ($data, $id, $problem) {

            //处理附件
            $this->attachService->updateAttaches($problem, $data['attaches']);
            //处理录屏
            $problem->records()->sync($data['record_ids']);


            $problem->fill($data)->save();
        });
        return $this->success(__('更新问题成功'));
    }


    public function detail(Request $request, $id)
    {

        $problem = Problem::query()
            ->with(['solutions.user:id,avatar,nick_name', 'solutions', 'solutions.attaches', 'attaches', 'user', 'records'])
            ->where('id', $id)
            ->firstOrFail();
        foreach ($problem['solutions'] as $key => $solution) {
            $problem['solutions'][$key]['resources'] = ($solution->recommends->isNotEmpty()) ? $this->recommendService->getRecommends('solution', $solution['id']) : [];
        }

        return $this->successData(compact("problem"));

    }

    /**
     *
     * 回答
     *
     * @param Request $request
     * @param Solution $solution
     * @return \Illuminate\Http\JsonResponse
     */


    public function submitSolution(SolutionRequest $request, Solution $solution)
    {

        $data = $request->filldata();
        $solution->fill($data)->save();
        $data['attaches'] && $solution->attaches()->createMany($data['attaches']);
        $data['recommends'] && $solution->recommends()->createMany($data['recommends']);
        return $this->success(__('发布回答成功'));

    }

    public function close($id)
    {

        $problem = Problem::query()->where('id', $id)->firstOrFail();
        $problem->update(['is_closed' => 1]);
        return $this->success(__('问题关闭'));

    }

    public function open($id)
    {
        $problem = Problem::query()->where('id', $id)->firstOrFail();
        $problem->update(['is_closed' => 0]);
        return $this->success(__('问题公开'));

    }

    public function delete($id)
    {
        $problem = Problem::query()->where('id', $id)->firstOrFail();
        $problem->delete();
        return $this->success(__('问题删除成功'));
    }




}